//
//  YGFoodTypeSelectView.m
//  yuanshangyiliao
//
//  Created by icholab on 2021/4/24.
//

#import "YGFoodTypeSelectView.h"
#import "YGFoodServerTypeModel.h"
#import "LHHorizontalPageFlowlayout.h"
#import "YGFoodServerTypeCollectionViewCell.h"
@interface YGFoodTypeSelectView()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSArray  *dataArray;
@property (nonatomic, strong) YGFoodServerTypeModel *lastModel;

@end

@implementation YGFoodTypeSelectView


- (void)configureModels:(NSArray <YGFoodServerTypeModel *> *)models{
    self.dataArray = models;
    [self.collectionView reloadData];
    
    [self collectionView:self.collectionView didSelectItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    YGFoodServerTypeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YGFoodServerTypeCollectionViewCell" forIndexPath:indexPath];
    YGFoodServerTypeModel *model = [self.dataArray objectAtIndex:indexPath.row];
    [cell configureModel:model];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (self.lastModel) {
        self.lastModel.isSelect = NO;
    }
    YGFoodServerTypeModel *model = [self.dataArray objectAtIndex:indexPath.row];
    model.isSelect = YES;
    [self.collectionView reloadData];
    self.lastModel = model;
    if (self.ClickItemBlock) {
        self.ClickItemBlock(model);
    }
}

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        layout.itemSize = CGSizeMake(80, 22);
        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
//        layout.minimumInteritemSpacing = -5;
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:YGFoodServerTypeCollectionViewCell.class forCellWithReuseIdentifier:@"YGFoodServerTypeCollectionViewCell"];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.layer.cornerRadius = 8;
        _collectionView.layer.borderWidth = 0.5;
        _collectionView.bounces = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.clipsToBounds = YES;
        _collectionView.layer.borderColor = UIColorFromRGB(0x37C8D7).CGColor;
        [self addSubview:_collectionView];
        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.bottom.right.mas_equalTo(self);
        }];
    }
    return _collectionView;
}
@end
